{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#### 结构化数组\n",
    "\n",
    "存储不再是一维二维数组，而是多种数据的组合。\n",
    "\n",
    "支持的数据类型表示形式\n",
    "\n",
    "| 数据类型 | 表示形式 |\n",
    "| :--------| :--------|\n",
    "| bytes | b1 |\n",
    "| int | i1, i2, i4, i8 |\n",
    "| unsigned ints | u1, u2, u4, u8 |\n",
    "| floats | f2, f4, f8 |\n",
    "| complex | c8, c16 |\n",
    "| fixed length string | a&lt;n&gt;(指定n个字符) |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 定义结构化数组\n",
    "\n",
    "1. 在定义的时候，指定dtype\n",
    "2. 定义之后，在指定dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "students: \n",
      " [(b'xiao ming',  9, 3,  True) (b'xiao hua', 10, 3,  True)\n",
      " (b'xiao_zhu',  8, 3, False) (b'xiao jie', 11, 4, False)]\n",
      "students.dtype: \n",
      " [('name', 'S12'), ('age', 'i1'), ('grade', 'i1'), ('sex', '?')]\n"
     ]
    }
   ],
   "source": [
    "# 在定义的时候,指定dtype\n",
    "students = np.array(\n",
    "    [(\"xiao ming\", 9, 3, 1),\n",
    "     (\"xiao hua\", 10, 3, 1),\n",
    "     (\"xiao_zhu\", 8, 3, 0),\n",
    "     (\"xiao jie\", 11, 4, 0)],\n",
    "    dtype=[('name', 'a12'),\n",
    "           ('age', 'i1'),\n",
    "           ('grade', 'i1'),\n",
    "           ('sex', 'b1')])\n",
    "print(\"students: \\n\", students)\n",
    "print(\"students.dtype: \\n\", students.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "students.dtype: \n",
      " [('id', 'S12'), ('order', 'i1'), ('value1', 'i1'), ('value2', '?')]\n"
     ]
    }
   ],
   "source": [
    "# 重新定义dtype中的属性，改写各个字段的名称\n",
    "students.dtype.names = (\"id\", \"order\", \"value1\", \"value2\")\n",
    "print(\"students.dtype: \\n\", students.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 访问结构化数组元素\n",
    "\n",
    "- 以索引的方式进行访问\n",
    "- 以类型标识进行访问\n",
    "- 综合索引和类型表示进行访问"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "students[1]: \n",
      " (b'xiao hua', 10, 3,  True)\n"
     ]
    }
   ],
   "source": [
    "# 以索引的方式进行访问\n",
    "print(\"students[1]: \\n\", students[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stdudents: \n",
      " [b'xiao ming' b'xiao hua' b'xiao_zhu' b'xiao jie']\n"
     ]
    }
   ],
   "source": [
    "# 以类型标识进行访问\n",
    "print(\"stdudents: \\n\", students[\"id\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "student:  b'xiao hua'\n",
      "student:  b'xiao hua'\n"
     ]
    }
   ],
   "source": [
    "# 综合索引和类型表示进行访问\n",
    "print(\"student: \", students[1][\"id\"])\n",
    "print(\"student: \", students[\"id\"][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
